package com.srs.dao.info;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.info.RxQuality;
import com.srs.util.HibernateUtil;

public class RxQualityDao {

	final static Logger logger = LoggerFactory.getLogger(RxQualityDao.class);

	private List<RxQuality> rxQualityList;
	private RxQuality rxQuality;

	public void delete(Integer agentId) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			rxQuality = (RxQuality) session.get(RxQuality.class, agentId);
			session.delete(rxQuality);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<RxQuality> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			rxQualityList = session.createQuery("from RxQuality").list();
			return rxQualityList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<RxQuality> list(Integer agentId, String fromDate, String toDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from RxQuality r where r.agentId=:agentId and r.onDate>=:fromDate and r.onDate<=:toDate");
			q.setInteger("agentId", agentId);
			q.setString("fromDate", fromDate);
			q.setString("toDate", toDate);
			rxQualityList = q.list();
			if (rxQualityList==null){
				return new ArrayList<RxQuality>();
			}
			return rxQualityList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}	
	
	public RxQuality get(Integer agentId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from RxQuality r where r.agentId=:agentId");
			q.setInteger("agentId", agentId);
			return (RxQuality) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public RxQuality get(Integer agentId, String onDate) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from RxQuality r where r.agentId=:agentId and r.onDate=:onDate");
			q.setInteger("agentId", agentId);
			q.setString("onDate", onDate);
			return (RxQuality) q.uniqueResult();
		} finally {
			session.close();
		}

	}
	
	
	public void insert(RxQuality rxQuality) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(rxQuality);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(RxQuality rxQuality) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(rxQuality);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

}
